package com.whut.gkd.service.impl;

import com.whut.gkd.bean.PageInfo;
import com.whut.gkd.bean.ShopInfo;
import com.whut.gkd.bean.User;
import com.whut.gkd.dao.IUserDao;
import com.whut.gkd.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements com.whut.gkd.service.IUserService {
    @Autowired
    private IUserDao userDao;

    @Override
    public boolean login(String username, String password) {
        User user = userDao.findUserByUsername(username);
        boolean flag = false;
        if(user != null && user.getPassword().equals(password)){
            flag = true;
        }
        return flag;
    }

    @Override
    public PageInfo<User> findAll(int currentPage, String username) {
        PageInfo pi = new PageInfo();
        pi.setSize(5);
        int totalcount = userDao.getTotalCount(username);
        double d = totalcount/5.0;
        int tp = (int) Math.ceil(d);
        pi.setTotalPage(tp);
        if(currentPage<1) currentPage = 1;
        if(currentPage>tp) currentPage = tp;
        pi.setCurrentPage(currentPage);
        int start = (currentPage-1)*5;
        List<User> userList = userDao.findAll(start, 5, username);
        pi.setList(userList);
        return pi;
    }

    @Override
    public void deleteById(int id) {
        userDao.deleteById(id);

    }

    @Override
    public void add(User user) {
        userDao.add(user);
    }

    @Override
    public User selectUserById(int id) {
        return userDao.selectUserById(id);
    }

    @Override
    public void update(User user) {
        userDao.update(user);
    }

    @Override
    public User findUserByName(String username) {
        return userDao.findUserByUsername(username);
    }

    @Override
    public void closeShop(ShopInfo shopinfo) {
        userDao.updateUserRole(shopinfo);
    }

    @Override
    public void deleteAll(List<Integer> ids) {
        userDao.deleteAll(ids);
    }


    //以下为新增
    @Override
    public void addUser(User user) {
        userDao.addUser(user);
    }

    @Override
    public void applyManager(int applicanId, String applicanName, String reason)
    {
        userDao.applyManager(applicanId,applicanName,reason);
    }

    @Override
    public void applyShop(int applicanId, String applicanName, String reason, String shopName, String shopDescription)
    {
        userDao.applyShop(applicanId, applicanName, reason, shopName, shopDescription);
    }


    @Override
    public void suspendedBYId(int id) {
        userDao.suspendedBYId(id);
    }

    @Override
    public void normalBYId(int id) {
        userDao.normalBYId(id);
    }

}
